package com.lrs.java.dataStructure.linkList;

class FirstLastList {

	Link first;
	Link last;
	
	FirstLastList() {
		first = null;
		last = null;
	}
	
	public void insertFirst(int iData, double dData) {
		Link newLink = new Link(iData, dData);
		
		if(isEmpty()) {
			last = newLink;
		} else {
			newLink.next = first;
		}
		first = newLink;
	}
	
	public void insertLast(int iData, double dData) {
		Link newLink = new Link(iData, dData);

		if(isEmpty()) {
			first = newLink;
		} else {
			last.next = newLink;
		}
		last = newLink;
		
	}
	
	public Link removeFirst() {
		Link temp = first;
		if(first.next == null) {
			last = null;
		}
		first = first.next;
		
		return temp;
	}

	public boolean isEmpty() {
		return first == null;
	}
	
	public void display() {
		Link current = first;
		
		while(current != null) {
			System.out.print("(" + current.getiData() + " " + current.getdData() + ")-->");
			current = current.next;
		}
		System.out.println();
	}
}


class FirstLastLinkApp {
	
	public static void main(String[] args) {
		
		FirstLastList link = new FirstLastList();
		link.insertFirst(11, 11.0);
		link.insertFirst(33, 33.0);
		link.insertFirst(55, 55.0);
		
		link.insertLast(22, 22.0);
		link.insertLast(44, 44.0);
		link.insertLast(66, 66.0);
		
		link.display();
		
		link.removeFirst();
		link.removeFirst();
		
		link.display();
		
	}
}
